//
//  CategoryHelper.m
//  cookBook
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "CategoryHelper.h"
#import "Model.h"
#import "CategoryModel.h"


@interface CategoryHelper ()

@end

@implementation CategoryHelper
//创建单例
+(instancetype)sharedManage
{
    static CategoryHelper * handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[CategoryHelper alloc] init];
    });
    return handle;
}


//分类的标题数组
-(NSArray *)categoryArray
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _categoryArray = @[@"荤菜",@"素菜",@"家常菜",@"西餐",@"主食粉面",@"粥",@"靓汤",@"特色小吃",@"早餐",@"酱料蘸料",@"烘焙",@"炖菜",@"烤箱",@"微波炉",@"时尚饮品",@"食疗药膳",@"甜品/糖水"];
    });
    return _categoryArray;
}

//功效的标题数组
-(NSArray *)effectArray
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _effectArray = @[@"补气养血",@"美容美颜",@"减肥瘦身",@"益智补脑",@"清热去火",@"健脾开胃",@"滋阴润燥",@"安神助眠",@"三高调养"];
    });
    return _effectArray;
}

//model数组
-(NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

-(CategoryModel *)categoryModel
{
    if (_categoryModel == nil) {
        _categoryModel = [[CategoryModel alloc] init];
    }
    return _categoryModel;
}

//根据头标题,页码,分类标题获取数据
-(void)fetchModelWithEffectOrCategory:(NSString *)effectOrCategory Page:(NSInteger)page Title:(NSString *)title block:(BLOCK)block
{
    NSString * urlString=nil;
    if ([effectOrCategory isEqualToString:@"分类"]) {
        urlString = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?category=%@&&p=%ld&pagesize=12",[title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],page];
    }else if ([effectOrCategory isEqualToString:@"功效"])
    {
        urlString = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?keywords=%@&&p=%ld&pagesize=12",[title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],page];
    }
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                                       {
                                           NSDictionary * rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                           
                                           [self.categoryModel setValuesForKeysWithDictionary:rootDic];
                                           
                                           NSArray * array = rootDic[@"results"];
                                           
                                           for (NSDictionary * dic in array) {
                                               
                                               Model * model = [[Model alloc] init];
                                               
                                               [model setValuesForKeysWithDictionary:dic];
                                               model.yuanliao = [self yuanliao:model.yuanliao];
                                               
     
                                               [self.modelArray addObject:model];
                                               
                                           }
                                           
                                               block();
                                          
                                           
                                       }];
    
    [dataTask resume];



}

//原料数据处理
- (NSString *)yuanliao:(NSString *)yuanliao
{
    NSArray *array = [yuanliao componentsSeparatedByString:@"</span>"];
    NSMutableString  *string = [NSMutableString string];
    
    for (NSString *string1 in array) {
        
        NSArray *array2 = [NSArray array];
        if (array.count == 1) {
            return string1;
        }else
        {
            array2 = [string1 componentsSeparatedByString:@"<span>"];
            
            
        }
        if (array2.count != 1) {
            [string appendString:array2[0]];
            [string appendString:array2[1]];

        }
    }
    
    
    return string;
    
}

-(NSArray *)returnModelArray
{
    return self.modelArray;
}
@end
